<?php

if (session_id() == '') {
    session_start();
}

 $logitud=false;
 if(!isset($_SESSION["kasutaja"])){
	if(isset($_POST['user'])and isset($_POST['pw'])){
		require_once('login_logout.php');
		login($_POST['user'], $_POST['pw']);
	}
}else{
	$logitud=true;
}
?>

<?php
if(!isset($_SESSION["kasutaja"])){
	if(isset($_POST['user'])and isset($_POST['pw'])){
		require_once('login_logout.php');
		$v=login($_POST['user'], $_POST['pw']);
	}
}else{
	$logitud=true;
}
if(isset($_REQUEST["logout"])){
	require_once('login_logout.php');
	logout();
	header("location: ".$_SERVER['PHP_SELF']);
}
?>

<!doctype html>
<html>
<head>
</head>
<body>
<div id="wrapper">
<?php require('pais.php');?>
<div id="leftcolumn">
	<div id="login">
		
		<?php if ($logitud==false){?>
		<h1>Log In</h1>
		<?php if (isset($v) and is_string($v))
		{
				echo"$v";
		} ?>
		<form method="post" action="?">
			<table>
			<tr>
				<td>Kasutaja <input type="text" name="user" /></td>
				<td>Parool <input type="password" name="pw"/></td>
				<td><input type="submit" name="Logi sisse" /></td>
			</tr>
			</table>
		</form>
	
	</div>
	<div id="Register">
		<a href="./register.php" id="reglink">Registreeru</a>		
	</div>
	<?php 
		}
		else{
			$flag=true;
			echo "<h2>Olete juba sisselogitud, suuname teid 2 sekundi pärast avalehele</h2>";
		} ?>
</div>
</div>
 <?php 
 if($logitud){
 	echo"<div id='rightcolumn'>";
	echo"<div id='tervitus'>";
	echo "Tere ".$_SESSION['nimi']."</div>";
	echo "<a href='?logout'>Logi välja</a>";
	echo "</div>";
	if ($flag){
			header('Location: ./ucp.php');
			exit();
	}	
 }?>
	
</div>
</body>
</html>